package com.itheima.health.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: itcast_health
 * @create: 2019-10-25 16:48
 * @author: li he
 * @description:
 *
 * {
 * idCard=123456789012345,
 * sex=1,
 * name=李贺,
 * setMealId=5,
 * validateCode=1434,
 * telephone=13899998888,
 * orderDate=2019-10-27}
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetMealDao setMealDao;
    @Override
    public Result add(Map<String, Object> map) throws ParseException {
        //获取参数
        //身份证号码
        String idCardParam = String.valueOf(map.get("idCard"));
        //性别
        String sexParam = String.valueOf(map.get("sex"));
        //姓名
        String nameParam = String.valueOf(map.get("name"));
        //电话号码
        String telephoneParam = String.valueOf(map.get("telephone"));
        //预约日期
        String orderDataStr = String.valueOf(map.get("orderDate"));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDateParam = simpleDateFormat.parse(orderDataStr);
        //套餐id
        Integer setMealIdParam = Integer.parseInt(String.valueOf( map.get("setMealId")));

        //0.判断输入日期是否为空
        if (orderDataStr == null || orderDataStr.length() == 0){
            return new Result(false, "日期不能为空");
        }

        //1.验证该日期是否进行预约设置 没有设置返回错误结果,
        OrderSetting orderSetting = orderSettingDao.findByDate(orderDataStr);
        if (orderSetting == null){
            return new Result(false, MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.判断该用户是否是会员 如果不是会员自动注册
        Member member = memberDao.findByTelephone(telephoneParam);
        if (member == null){
            member = new Member();
            member.setName(nameParam);
            member.setSex(sexParam);
            member.setIdCard(idCardParam);
            member.setPhoneNumber(telephoneParam);
            member.setRegTime(orderDateParam);
            //注册时要(主键回显)
            memberDao.add(member);
        } else { //如果已经注册会员
            //3.判断是否重复预约(同一天,同一个会员,同一个套餐) 去订单表里边查
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderDateParam);
            order.setSetmealId(setMealIdParam);

            long count = orderDao.findCountByCondition(order);

            if (count > 0){ //重复预约
               return new Result(false,MessageConst.HAS_ORDERED);
            }
        }
        //4.判断该日期是否预约已满
        int numberDb = orderSetting.getNumber();
        int reservationsDb = orderSetting.getReservations();
        if (reservationsDb >= numberDb){
            return new Result(false,MessageConst.ORDER_FULL);
        }
        //5.添加预约信息
        Order orderAdd = new Order();
        //会员ID
        orderAdd.setMemberId(member.getId());
        //预约日期
        orderAdd.setOrderDate(orderDateParam);
        //套餐ID
        orderAdd.setSetmealId(setMealIdParam);
        //订单类型
        orderAdd.setOrderType(Order.ORDERTYPE_WEIXIN);
        //订单状态
        orderAdd.setOrderStatus(Order.ORDERSTATUS_NO);

        // 要加主键回显
        orderDao.add(orderAdd);
        //6.更新预约设置
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.edit(orderSetting);

        return new Result(true,MessageConst.ORDER_SUCCESS,orderAdd);
    }

    @Override
    public Map<String, Object> findById(Integer id) {

        Map<String, Object> map = orderDao.findById(id);

        Date date = (Date) map.get("orderDate");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = simpleDateFormat.format(date);
        map.put("orderDate",dateStr);

        return map ;
    }

    @Override
    public Map<String, Object> getSetMealReport() {

        List<Setmeal> setmealList = setMealDao.findAll();

        ArrayList<String> setMealNames = new ArrayList<>();
        ArrayList<Object> setMealMapList = new ArrayList<>();

        for (Setmeal setmeal : setmealList) {
            setMealNames.add(setmeal.getName());

           long count = orderDao.findOrderCountBySetMealId(setmeal.getId());

            Map<String,Object> setMealMap = new HashMap<>();

            setMealMap.put("name",setmeal.getName());
            setMealMap.put("value",count);

            setMealMapList.add(setMealMap);
        }

        HashMap<String, Object> totalMap = new HashMap<>();
        totalMap.put("setMealMapList",setMealMapList);
        totalMap.put("setMealNames",setMealNames);

        return totalMap;
    }
}